var express = require('express')
var url = require('url')
var template = require('art-template')
var fs = require('fs')

var app = express()

var comments = [
	{
		name: '银永鑫',
		message: '这是自己做的第一个小服务器',
		dateTime: '2019-7-10'
	}
]


app.get('/',function(request,response){
	fs.readFile('./views/index.html',function(err,data){
		//使用express就不用if了
		var htmlStr = template.render(data.toString(),{
			comments:comments
		})
		response.end(htmlStr)
	})
})

app.get('/index',function(request,response){
	fs.readFile('./views/index.html',function(err,data){
		var htmlStr = template.render(data.toString(),{
			comments:comments
		})
		response.end(htmlStr)
	})
})

app.get('/post',function(request,response){
	fs.readFile('./views/post.html',function(err,data){
		response.end(data)
	})
})

app.get('/a',function(request,response){
	fs.readFile('./views/a.html',function(err,data){
		response.end(data)
	})
})

app.get('/pinglun',function(request,response){

	var parseObj = url.parse(request.url,true)
	var comment = parseObj.query

	var dateTime = new Date();
    var year = dateTime.getFullYear()
    var month = dateTime.getMonth() + 1
    var day = dateTime.getDate()
    comment.dateTime = year + '-' + month + '-' + day 


    comments.unshift(comment)
    

    response.statusCode = 302
    response.setHeader('location','/')
    response.end()
})

app.use('/public/', express.static('./public/'))

app.listen(3000,function(){
	console.log('running')
})